﻿(function(window, undefined) {

	var puman = window.puman = window.puman || {};
	// opUrl
	puman.opUrl = "service/tagerOp.ashx";
	// build
	puman.build = build;

	function build() {
		var config = puman.config;
		var bd = $('body');
		var head = $('#head');
		var foot = $('#foot');
		// head
		var rowHead = $('<div/>').addClass('row-fluid');
		var nav = $('<div/>').addClass('span6 offset1');
		var navUl = $('<ul/>').addClass('nav').addClass('nav-pills');
		rowHead.append(nav);
		nav.append(navUl);
		head.append(rowHead);
		$.each(config.head.navs, function(i, n) {
			var li = $('<li/>');
			var a = $('<a/>');
			a.attr('href', n.href).text(n.name);
			li.append(a);
			navUl.append(li);

		});


		var href = window.location.href;
		var pageName = href.substring(href.lastIndexOf('/') + 1);


		var index = -1;
		$.each(config.head.navs, function(i, n) {
			if(n.href == pageName) {
				index = i;
				return false;
			}
		});

		if(index != -1) {
			navUl.find('li').removeClass('active');
			navUl.find('li').eq(index).addClass('active');
		}
	}

	// userInfo
	puman.getUserInfo = getUserInfo;

	function getUserInfo(nextFunc) {
		var url = puman.opUrl;
		var data = {};
		var callback = function(data) {
				puman.userInfo = format(data);

				// 回调
				nextFunc && nextFunc();

				// 格式化数据

				function format(data) {
					var rst = null;
					return rst;
				}
			};

		$.post(url, data, callback);
	}

	puman.appBar = (function() {
		var util = {};
		// 初始化util
		util.init = function() {
			var appBar = $('#appBar');
			// 事件格式:appBar_hotVideo
			appBar.delegate('li', 'click', function(e) {
				var me =$( this);
				me.css('opacity', '1');
				me.siblings().css('opacity', '.4');
				// 触发app
				var ptag=me.attr('ptag');
				puman.trigger(['appBar',ptag].join('_'));
				console.log('click the appBar');
			});
			// tooltip
			appBar.find('li').each(function(i,n){
				var me=$(this);
				var tip=me.attr('tooltip');
				me.tooltip({title:tip});

			})
		};

		return util;
	})();

	puman.__eventList = {};
	// 触发事件
	puman.trigger = function(eventName, opts) {
		var el = puman.__eventList;
		if(el[eventName]) {
			for(var i = 0; i < el[eventName].length; i++) {
				var eli = el[eventName][i];
				eli(opts);
			}
		}

	};
	// 添加事件
	puman.attach = function(eventName, fn) {

		var el = puman.__eventList;
		el[eventName] = el[eventName] || [];
		el[eventName].push(fn);
	};

	puman.util=(function(){
		var util={
			formatDate:function(jsonDate){
				var num=jsonDate.match(/\d+/)[0];
				return new Date(num);
			}
		};
		return util;

	})();


})(window);


$(puman.build);